/**
 * Created by apple on 16/3/17.
 */
import './style.scss'
class Controller {
    constructor (systemService, statusMap, uiNotification, item, $modalInstance, plat) {
        "ngInject"
        this.notify    = uiNotification;
        this.uiModal   = $modalInstance;
        this.service   = systemService;
        this.select    = {status: statusMap, plats: plat.data};
        this.item      = item;
        this.data      = item ? angular.copy(item) : {statusCd: "S0A"};
        this.type      = item ? 'update' : 'insert';
        this.isEdit    = item ? true : false;
        this.query     = false;
        this.region    = {
            commonRegionId: item ? item.commonRegionId : '',
            parRegionId   : 0,
            statusCd      : 'S0A'
        }
        this.regionMap = {
            1000: 'group',
            1100: 'province',
            1300: 'city',
            1400: 'area'
        }
        this.getRegion()
    }

    getArea (value) {
        this.data.commonRegionId = value
    }

    getRegion (type = 1000, value = null) {
        if ( type ) {
            this.region.regionType = type
        }
        if ( value ) {
            this.data.commonRegionId = value.id ? value.id : value;
            this.region.parRegionId  = value.id ? value.id : value;
        }

        if (this.query) {
            this.isEdit = false;
            this.region.qFlag = 'query';
            delete this.region.commonRegionId;
        }

        this.service.getRegion(this.region, this.isEdit).then((r) => {
            __DEBUG__ && console.log(r)
            if ( r.data ) {
                if ( !type ) {
                    this.select.group       = r.data;
                    this.region.parRegionId = r.data[ 0 ].id
                }
                if ( this.isEdit ) {
                    this.query = true;
                    this.select.group    = r.data[ '1000' ] ? r.data[ '1000' ].data : '';
                    this.select.province = r.data[ '1100' ] ? r.data[ '1100' ].data : '';
                    this.select.city     = r.data[ '1300' ] ? r.data[ '1300' ].data : '';
                    this.select.area     = r.data[ '1400' ] ? r.data[ '1400' ].data : '';
                    if ( !value ) {
                        this.group    = r.data[ '1000' ] ? r.data[ '1000' ].select : '';
                        this.province = r.data[ '1100' ] ? r.data[ '1100' ].select : '';
                        this.city     = r.data[ '1300' ] ? r.data[ '1300' ].select : '';
                        this.area     = r.data[ '1400' ] ? r.data[ '1400' ].select : '';
                    }
                } else {
                    if ( !r.data.length ) {
                        this.select[ this.regionMap[ type ] ] = null;
                    } else {
                        this.select[ this.regionMap[ type ] ] = r.data;
                    }
                }
            }
        })


    }

    checkValid (field) {
        if ( field.$pristine ) {
            field.$pristine = false;
        }
    }

    save () {
        if ( this.submitting ) return;
        this.submitting = true;
        if ( this.item ) {
            delete this.data.commonRegion;
            delete this.data.platInfo;
        }
        this.service[ this.type ]('role', this.data)
            .then((r) => {
                let region;
                if (this.group) {
                    region = _.find(this.select.group, {id: this.group})
                }
                if (this.province) {
                    region = _.find(this.select.province, {id: this.province})
                }
                if (this.city) {
                    region = _.find(this.select.city, {id: this.city})
                }
                if (this.area) {
                    region = _.find(this.select.area, {id: this.area})
                }
                this.data.commonRegion = {
                    regionName : region.regionName
                }
                if (!this.item) {
                    this.data.sysRoleCode = 'R' + r.data;
                } else {
                    let item = _.find(this.select.plats, {platSysNbr: this.data.sysCode});
                    this.data.platInfo = {
                        platName : item['platName']
                    }
                }
                this.uiModal.close(this.data);
                this.notify.success({
                    'title'  : '提示',
                    'message': '操作成功!'
                });
            }).catch((data) => {
                this.notify.error({
                    'title'  : '提示',
                    'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
                });
            }).finally(() => {
                // 隐藏 loading
                this.submitting = false;
            });
    }
    cancel () {
        this.uiModal.dismiss('cancel');
    }
}

export default Controller;